﻿using Pay.Core;
using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pay.Web.Areas.Admin.Controllers
{
    [RouteArea("admin")]
    [RoutePrefix("consumeItem")]
    public class ConsumeItemController : AdminController
    {
        private IConsumeItemService _iConsumeItemService;

        public ConsumeItemController(IConsumeItemService iConsumeItemService)
        {
            _iConsumeItemService = iConsumeItemService;
        }

        public ActionResult Index()
        {
            return View();
        }

        #region 消费项目
        public JsonResult Gets(ConsumeItemQuery query)
        {
            var model = _iConsumeItemService.GetConsumeItems(query);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        public JsonResult Get(long id)
        {
            consume_item_dto_ex model = new consume_item_dto_ex() { consume_type = EConsumeItemType.RechargeOilCard, apply = EConsumeItemApply.APP, disabled_value = "0", img_url = "" };//新增时也要获得任务列表下拉数据
            if (id > 0)
                _iConsumeItemService.GetConsumeItem(id).ToCopyProperty(model);
            model.consume_type_value = ((int)model.consume_type).ToString();
            model.apply_value = ((int)model.apply).ToString();
            model.disabled_value = model.disabled ? "1" : "0";
            model.url = model.url ?? "";
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Add(consume_item_dto_ex dto)
        {
            dto.consume_type = (EConsumeItemType)int.Parse(dto.consume_type_value);
            dto.apply = (EConsumeItemApply)int.Parse(dto.apply_value);
            dto.disabled = dto.disabled_value == "1" ? true : false;
            var file_url = Upload("ConsumeItem");
            if (!string.IsNullOrEmpty(file_url))
                dto.img_url = file_url;
            else
                return Json(ResultToJson.Json(1001, "请先上传图片", new { }), JsonRequestBehavior.AllowGet);
            var result = _iConsumeItemService.AddConsumeItem(dto);
            return Json(result, JsonRequestBehavior.AllowGet);

        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Update(consume_item_dto_ex dto)
        {
            dto.consume_type = (EConsumeItemType)int.Parse(dto.consume_type_value);
            dto.disabled = dto.disabled_value == "1" ? true : false;
            dto.apply = (EConsumeItemApply)int.Parse(dto.apply_value);
            var file_url = Upload("ConsumeItem");
            if (!string.IsNullOrEmpty(file_url))
                dto.img_url = file_url;

            var result = _iConsumeItemService.UpdateConsumeItem(dto);
            return Json(result, JsonRequestBehavior.AllowGet);

        }

        [HttpPost]
        public JsonResult UpdateDisabled(long id, int isDisabled)
        {
            var result = _iConsumeItemService.UpdateDisabled(id, isDisabled);
            return Json(ResultToJson.Json(result > 0 ? " 修改成功" : "修改失败", result));
        }
        #endregion

        #region 消费-商品兑换
        public JsonResult GetConsumeProducts(ConsumeProductQuery query)
        {
            var model = _iConsumeItemService.GetConsumeProducts(query);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetConsumeProduct(long id)
        {
            consume_product_dto_ex model = new consume_product_dto_ex() { status = EConsumeProductStatus.Putaway, img_url = "" };
            if (id > 0)
                _iConsumeItemService.GetConsumeProduct(id).ToCopyProperty(model);
            model.status_value = ((int)model.status).ToString();//ng-select 需要绑定字符串类型
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult AddConsumeProduct(consume_product_dto_ex dto)
        {
            dto.status = (EConsumeProductStatus)int.Parse(dto.status_value);
            var file_url = Upload("ConsumeProduct");
            if (!string.IsNullOrEmpty(file_url))
                dto.img_url = file_url;
            else
                return Json(ResultToJson.Json(1001, "请先上传图片", new { }), JsonRequestBehavior.AllowGet);
            var result = _iConsumeItemService.AddConsumeProduct(dto);
            return Json(ResultToJson.Json(result > 0 ? " 保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);

        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult UpdateConsumeProduct(consume_product_dto_ex dto)
        {
            dto.status = (EConsumeProductStatus)int.Parse(dto.status_value);
            var file_url = Upload("ConsumeProduct");
            if (!string.IsNullOrEmpty(file_url))
                dto.img_url = file_url;

            var result = _iConsumeItemService.UpdateConsumeProduct(dto);
            return Json(ResultToJson.Json(result > 0 ? " 保存成功" : "未做任何修改", result), JsonRequestBehavior.AllowGet);

        }

        [HttpPost]
        public JsonResult UpdateConsumeProductStatus(long id, int status)
        {
            var result = _iConsumeItemService.UpdateConsumeProductStatus(id, status);
            return Json(ResultToJson.Json(result > 0 ? " 修改成功" : "修改失败", result));
        }
        #endregion

        #region 消费记录
        public JsonResult GetUserComsumeHiss(UserComsumeHisQuery query)
        {
            var model = _iConsumeItemService.GetUserComsumeHiss(query);
            return Json(ResultToJson.Json(new { e_consume_type_list = typeof(EConsumeItemType).GetDropdownItemList(), data = model }), JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult UpdateUserComsumeHisStatus(long id, int status) {
            var result = _iConsumeItemService.UpdateUserComsumeHisStatus(id, status);
            return Json(ResultToJson.Json(result > 0 ? " 修改成功" : "修改失败", result));
        }
        #endregion


    }
}